//
//  QMessageVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/7/8.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QMessageVC.h"
#import "QMessageCell.h"
#import "QMessageModel.h"
#import "MJExtension.h"
#import "QLoginVC.h"
#import "QMessageListVC.h"
#import "UITabBar+badge.h"
#import "OrderStatusVC.h"
#import "AllOrderMessageVC.h"
@interface QMessageVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _index;
    NSMutableArray *_dataArr;
}

@property (nonatomic,strong)UITableView *tbView;
@end

@implementation QMessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"消息";
    [self createUI];
    
    _dataArr = [NSMutableArray array];
    
   // [self requestData];
    
    _tbView.mj_header = [QViewUtility showHeadRefresh:^{
        [_tbView.mj_footer resetNoMoreData];
        [self requestData];
    }];
    
    _tbView.mj_footer = [QViewUtility showFootRefresh:^{
        [self requestMoreData];
    }];
    
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:kNotificationTabBar3 object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)requestData{
    _index = 0;
    NSString *start = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getTypeList"];
    NSDictionary *para = [QParamService getTypeListWithMachineId:[QParamService getUUID] Start:start PageSize:KpageSize];
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [QViewUtility disMissNoRecordInVC:self];
    [QViewUtility disMissNoNetWorkInVC:self];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_header endRefreshing];
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tbView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [_dataArr removeAllObjects];
            
            
            _dataArr = [QMessageModel mj_objectArrayWithKeyValuesArray:[basModel.items objectForKey:@"list"]];
//            QMessageModel *model = [[QMessageModel alloc]init];
//            model.imgUrl = @"";
//            model.isDelete = @"0";
//            model.ptName = @"订单通知测试";
//            model.data = @"下单成功";
//            model.ptType = @"100";
//            model.ptId = @"100";
//            model.addTime = @"2017-12-09 09:09:34";
//            model.isLook = @"1";
//            [_dataArr addObject:model];
            if(_dataArr.count == 0){
                [QViewUtility showNoRecordViewInView:_tbView Title:@"没有任何消息哦~"];
            }else{
                if(_dataArr.count < 10){
                    [_tbView.mj_footer noticeNoMoreData];
                }
                NSMutableArray * arr = [NSMutableArray arrayWithArray:_dataArr];
                for (QMessageModel *model in arr) {
                    if ([model.ptType isEqualToString:@"4"]) {
                        [_dataArr removeObject:model];
                    }
                }
            }
            [_tbView reloadData];
            
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

- (void)requestMoreData{
    _index++;
    NSString *start = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getTypeList"];
    NSDictionary *para = [QParamService getTypeListWithMachineId:[QParamService getUUID] Start:start PageSize:KpageSize];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_footer endRefreshing];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        
        if([basModel.result isEqualToString:@"success"]){
            NSArray * arr = [QMessageModel mj_objectArrayWithKeyValuesArray:[basModel.items objectForKey:@"list"]];
            [_dataArr addObjectsFromArray:arr];
            if(arr.count < 10){
                [_tbView.mj_footer noticeNoMoreData];
            }
            [_tbView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QMessageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QMessageCell" forIndexPath:indexPath];
    cell.model = [_dataArr objectAtIndex:indexPath.section];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    QMessageModel * model = [_dataArr objectAtIndex:indexPath.section];

//    if ([model.ptType isEqualToString:@"100"]) {
//        AllOrderMessageVC *vc = [[AllOrderMessageVC alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
//        return;
//    }
    if ([model.ptType isEqualToString:@"1"]) {//跳转订单通知
        AllOrderMessageVC *vc = [[AllOrderMessageVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
//        OrderStatusVC *vc = [[OrderStatusVC alloc]init];
//        vc.title = model.ptName;
//         WS(wSelf);
//        vc.block = ^{
//            [wSelf requestData];
//        };
//        [self.navigationController pushViewController:vc animated:YES];
    }else{
    
        QMessageListVC * vc = [[QMessageListVC alloc] init];
        vc.title = model.ptName;
        WS(wSelf);
        vc.block = ^{
            [wSelf requestData];
        };
        vc.ptType = model.ptType;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
   
}

- (void)createUI{
    [self.view addSubview:self.tbView];
    self.tbView.frame = CGRectMake(12, 0, kScreenWidth - 24, kScreenHeight );
    [self.tbView registerNib:[UINib nibWithNibName:@"QMessageCell" bundle:nil] forCellReuseIdentifier:@"QMessageCell"];
    
}
#pragma mark - Getter/Setter
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
